﻿using System.Data;
using System.Diagnostics;
using System.IO;

using Gamey.NES;
using Gamey.Genesis;
using Gamey.SNES;
using Gamey.N64;

namespace Gamey
{
    public abstract class Emulator
    {
        static readonly Emulator nestopia = new Nestopia();
        static readonly Emulator gens = new Gens();
        static readonly Emulator zsnes = new ZSNES();
        static readonly Emulator project64 = new Project64();

        public static Emulator Nestopia
        {
            get { return nestopia; }
        }

        public static Emulator Gens
        {
            get { return gens; }
        }

        public static Emulator ZSNES
        {
            get { return zsnes; }
        }

        public static Emulator Project64
        {
            get { return project64; }
        }

        protected abstract GameLoader GameLoader { get; }

        protected abstract string ExecutablePath { get; }

        public abstract string RomsPath { get; }

        public void LoadGames(GameyDataSet dataSet)
        {
            GameLoader.LoadGames(GetTableFromDataSet(dataSet));
        }

        public abstract DataTable GetTableFromDataSet(GameyDataSet dataSet);

        public virtual void LaunchGameFromDataRow(DataRow row)
        {
            string exePath = ExecutablePath;

            var startInfo = new ProcessStartInfo
            {
                FileName = exePath,
                Arguments = GetArgumentsFromDataRow(row),
                WindowStyle = ProcessWindowStyle.Maximized,
                WorkingDirectory = Path.GetDirectoryName(exePath)
            };

            Process.Start(startInfo);
        }

        protected virtual string GetArgumentsFromDataRow(DataRow row)
        {
            return string.Format("\"{0}\"", row.Field<string>("File"));
        }
    }
}
